//
//  LikerListViewController.m
//  TourPlay
//
//  Created by dllo on 15/11/24.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "LikerListViewController.h"
#import "LikeModel.h"
#import "LikerListCell.h"

@interface LikerListViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *likeTable;

@property (nonatomic, assign) BOOL isUp;
@property (nonatomic, retain) MBProgressHUD *hub;
@property (nonatomic, retain) NSMutableArray *likeArray;

@property (nonatomic, assign) NSInteger pIndex; // 上拉加载页数
@property (nonatomic, retain) NSMutableArray *upArray; // 是否已经上拉加载完了
@end

@implementation LikerListViewController
- (void)dealloc
{
    [_likeTable release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"感兴趣的人";
    self.isUp = NO;
    self.likeArray = [NSMutableArray array];
    self.upArray = [NSMutableArray array];
    
    self.likeTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.likeTable.delegate = self;
    self.likeTable.dataSource = self;
    self.likeTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.likeTable.backgroundColor = [UIColor clearColor];
    [self.likeTable registerClass:[LikerListCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.likeTable];
    [_likeTable release];


    [self addHeaderRefresh];
    [self addFooterRefresh];
    

    [self getUpdate];
}
-(void)addHeaderRefresh
{
    self.likeTable.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUp = NO;
        self.pIndex = 0;
        [self getUpdate];
    }];
}
-(void)addFooterRefresh
{   // 当上拉时候触发该方法
    self.likeTable.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.isUp = YES;
        self.pIndex++;
        [self getUpdate];
    }];
}
- (void)leftAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70.f;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.likeArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellStr = @"cell";
    LikerListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    
    cell.likeMod = [self.likeArray objectAtIndex:indexPath.row];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [self addColorChangedBlock:^{
        
        cell.contentView.nightBackgroundColor = [ColorSingleton shareSingleton].cellColor;
        cell.contentView.normalBackgroundColor = [UIColor whiteColor];
    }];
    
    
    return cell;
}

- (void)getUpdate
{
    // 让菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"努力加载中...";
    
    NSString *url = [NSString stringWithFormat:@"http://wanapi.damai.cn/commodity/getlikerlist.json?cid=%@&pindex=%ld&psize=20&source=%d&version=%d", self.activityId, self.pIndex, URL_SOURCE, URL_VERSION];
    
    [AppTools getAFNet:url block:^(id result) {
        [self.upArray removeAllObjects];
        
        // 如果是下拉
        if (!self.isUp) {
            [self.likeArray removeAllObjects];
        }
        NSMutableDictionary *bigDic = result;
        
        for (NSMutableDictionary *dic in [[bigDic objectForKey:@"data"] objectForKey:@"list"]) {
            LikeModel *likeMod = [LikeModel modelWithDictionary:dic];
            [self.likeArray addObject:likeMod];
            [self.upArray addObject:likeMod];
        }
        
         [self.likeTable reloadData];
        
        if (self.upArray.count < 20) {
            // 变为没有更多数据的状态
            //            [self.smallTableV.footer endRefreshingWithNoMoreData];
            // 没有数据隐藏上拉
            self.likeTable.footer.hidden = YES;
        }
        
    

       
        //结束下拉上拉刷新
        [self.likeTable.header endRefreshing];
        [self.likeTable.footer endRefreshing];
        
        // 让菊花停止旋转
        [self.hub hide:YES];
    } errorBlock:^(NSError *error) {
        
        NSLog(@"网络错误");
        // 让菊花停止旋转
        [self.hub hide:YES];
        
        //结束下拉上拉刷新
        [self.likeTable.header endRefreshing];
        [self.likeTable.footer endRefreshing];
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
